package net.stogi.dsper.ui.client.module.login;

import net.stogi.common.mvp.core.behavior.Behavior;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayoutData;
import com.extjs.gxt.ui.client.widget.layout.HBoxLayout.HBoxLayoutAlign;

class LoginInfoWidget extends Composite implements LoginInfoView {

	private static final HBoxLayoutData DEFAULT_FLOW_DATA = new HBoxLayoutData(new Margins(2, 5, 0, 2));

	private final Button logoutButton = new Button();

	private final ContentPanel mainPanel = new ContentPanel();

	private final TextField<String> nameTextField = new TextField<String>();

	public LoginInfoWidget() {
		initComposite();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void appear() {
		mainPanel.show();
		// mainPanel.el().fadeIn(FxConfig.NONE);
	}

	@Override
	public void block() {
		mainPanel.mask();
	}

	@Override
	public void disappear() {
		// mainPanel.el().fadeOut(FxConfig.NONE);
		mainPanel.hide();
	}

	@Override
	public void displayName(String name) {
		// nameLabel.setText(name);
		nameTextField.setValue(name);
	}

	@Override
	public void setLogoutBehavior(final Behavior behavior) {
		logoutButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void unblock() {
		mainPanel.unmask();
	}

	private void initComposite() {
		logoutButton.setText("Logout");
		nameTextField.setEnabled(false);

		mainPanel.setHeaderVisible(false);
		mainPanel.setFrame(true);
		mainPanel.setWidth(215);
	}

	private void initLayout() {
		HBoxLayout rowLayout = new HBoxLayout();
		rowLayout.setHBoxLayoutAlign(HBoxLayoutAlign.MIDDLE);

		LayoutContainer rowContainer = new LayoutContainer(rowLayout);
		rowContainer.add(nameTextField, DEFAULT_FLOW_DATA);
		rowContainer.add(logoutButton);

		mainPanel.add(rowContainer);
	}
}
